<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<meta name="author" content="lushu">
		<title>单位模板维护</title>
		<script type="text/javascript" language="javascript" src="../script/common.js"></script>
		<script type="text/javascript" language="JavaScript" src="../script/tree.js"></script>
		<script type="text/javascript" language="JavaScript">
			var tree;
			var Page;
			
			function load(){
				Page = {};
				Page.changed = false;//页面模板是否有改动
				setFlag(false);//Page.flag = false;//是否有未保存内容
				Page.newDT = {};//记录模板变动
				Page.form = $("listForm");
				Page.templateId = getPara("templateId");//当前的模板ID
				if (!Page.templateId){
					showWarning("没有找到模板信息");
					feedback();
				}
				var v = DWR.syncCall("txQueryService.loadDistributeTemplate",Page.templateId);
				if(v){
					Page.cardUserId = v.cardUserId;
					Page.templateName = v.name;
					Page.lastModified = v.lastModified;
					Page.distribute = v.distribute;//分配、汇总方式
					Page.accCode = v.accCode;
					Page.dt = v.das;
				}

				DWR.call("mgntQueryService.getCardUser", Page.cardUserId,
						function(res){
							if(res){
								writeTree({id:res.cardUser.id,name:res.companyName});
							}
						}
					);
				Page.outTransformer = Account.Transformer.OutByAccCode(Page.accCode);
				Page.inTransformer = Account.Transformer.InByAccCode(Page.accCode);//向系统内转换的函数
				Page.queryData = {	orgUserId:Page.cardUserId,
									accCode:Page.accCode,
									distribute:Page.distribute};//查询司机的参数
				initQueryTable();
				updateDtInfo();
				Page.qt.showTable();
			}
			//树函数
			function writeTree(obj){
				obj.isRoot = true;
				tree = new LoadTree(obj.name, null,"javascript:selectNode()",null,"../images/tree/root.png","../images/tree/root.png");
				tree.obj = obj
				$("depttree").innerHTML = tree;
			}
			//树函数
			function _startLoadXmlTree(sSrc, jsNode) {
				var params = {}
				if (sSrc){
					params.cardUserId = Page.cardUserId;
					params.parentId = sSrc;
				}else{
					params.nullParent = Page.cardUserId;
				}
				DWR.call("ejbQueryService.findData","find_dept",params,function(result){
					_xmlFileLoaded(result,jsNode);
				});
			}
			//树函数
			function _createNode(r){
				var obj = {id:r[0],name:r[1]}
				var node = createTreeNode(false,obj.name,obj.id,"javascript:selectNode()");
				node.obj = obj;
				return node;
			}
			//选中某个节点后的操作
			function selectNode(){
				if (!checkSameNode()){
					confirmLeave();
					var node = tree.getSelected().obj;
					if (node.isRoot){//根目录
						Page.queryData.departmentList = '';
						Page.lastNode = "root";
					}else{//非根目录typeof node.id != "object"
						Page.queryData.departmentList = node.id;
						var result = DWR.syncCall("customerService.getChildDepts",Page.cardUserId,node.id);
						if(result){
							Page.queryData.departmentList += ','+result;
							Page.lastNode = node.id;
						}
					}
					Page.qt.showTable();
				}
			}
			//判断所选择的节点是否为原来的节点
			function checkSameNode(){
				if (!Page.lastNode)
					return false;
				var node = tree.getSelected().obj;
				if (typeof node.id == "object")
					node = "root"
				else
					node = node.id
				return (Page.lastNode==node);
			}
			//离开某个页面时,如果发现页面有改动,首先提示用户是否保存
			function confirmLeave(){
				checkChanges();
				if(Page.flag){
					var saveFlag;
					saveFlag = showQuestion("模板有改动,需要保存吗?");
					if (saveFlag)
						saveTemplate();
					else{
						setFlag(false);
						Page.newDT = {};
					}
				}
			}
			//初始化QueryTable
			function initQueryTable(){
				var inputRender = function(v,r){
					var str = "<input type='text' class='templateAmount' onblur='checkAmount(this)'";
					if (Page.dt[r[0]])
						str += "value='"+Page.outTransformer(Page.dt[r[0]])+"'";
					return str+"name='amount"+r[0]+"'>";
				}
				var columns = [];
				columns[0] = {type:"checkbox"};
				columns[7] = {render:inputRender,order:false,width:'25%'};
				Page.qt = new QueryTable("tx_list_drivers",null,"datalist",columns,
						{paramFunc:function(){
								Page.queryData.cardAsn = Page.form.cardAsn.value;
								Page.queryData.customerName = Page.form.customerName.value;
								return Page.queryData;
							},
						buttons:[
							{value:"清 空",actions:clearTemplate},
							{value:"保 存",actions:saveTemplate},
							{value:"另存为...",actions:saveAs},
							{value:"批量设置",actions:batchSet}
						]});
				
			}
			function checkAmount(obj){
				var key = obj.name.substring('amount'.length);
				if(!checkV(obj,"nonNegativeAmount")){
					var tmp = Page.outTransformer(Page.dt[key]);
					showWarning("输入的金额错误："+obj.value);
					obj.value = tmp;
					clearErrorView(Page.form.id);
					checkChanges();
				}else{
					var tmp = Page.inTransformer(obj.value);
					if((Page.dt[key] && tmp!=Page.dt[key]) || (!Page.dt[key] && tmp!=0)){
						Page.newDT[key] = tmp?tmp:0;
						setFlag(true);
					}
				}
			}
			//清空模板
			function clearTemplate(){
				if (showQuestion("确定要清空当前的模板吗？")){
					Page.newDT = {};
					for(var x in Page.dt)
						if(typeof(Page.dt[x])!="function"){
							var name = "amount"+x;
							if (Page.form[name])
								Page.form[name].value = '';
						}
					setFlag(true);
					saveTemplate();
					Page.qt.showTable();//重新载入页面
					setFlag(false);
				}
			}
			//批量设置金额
			function batchSet(){
				var ids = Page.qt.getBoxValue().split(',');
				if (ids.length==0 || ids[0]==""){
					showWarning("请先选择需要批量设置的司机");
					return;
				}
				var amount = prompt("请输入为每个司机设置的金额:",0);
				if (amount){
					if (!(amount-0) && amount!=0){
						showWarning('输入的金额无效:'+amount);
						return;	
					}
					for (var x in ids){
						if (typeof ids[x]!="function" && Page.form["amount"+ids[x]])
								Page.form["amount"+ids[x]].value = amount;
					}
					checkChanges();
				}
			}
			//检查当前的form中有无改动
			function checkChanges(){
				var node = Common.getValues(Page.form);
				for(var x in node){
					if (typeof node[x]!="function" && x.indexOf("amount")>-1){
						var key = x.substring("amount".length);
						node[x] = Page.inTransformer(node[x]);
						if((Page.dt[key] && node[x]!=Page.dt[key]) || (!Page.dt[key] && node[x]!=0)){
							Page.newDT[key] = node[x]?node[x]:0;
							setFlag(true);
						}
					}
				}
			}
			//保存模板
			function saveTemplate(){
				checkChanges();
				if(!checkForm(Page.form.id))
					return;
				if (Page.flag){
					if (!Page.templateId){
						saveAs();
					}
					else{
						var result = DWR.syncCall("txQueryService.updateDistributeTemplate",Page.templateId,Page.newDT);
						Page.templateName = result.name;
						Page.lastModified = result.lastModified;
						updatePageDt();
						Page.changed = true;
					}
				}
			}
			//更新模板提示信息
			function updateDtInfo(){
				$('templateName').innerHTML = Page.templateName;
				$('lastModified').innerHTML = Format.timeRender(Page.lastModified);
			}
			//更新Page.dt内容:
			//1.当创建一个新模板时需要先合并修改到Page.dt中
			//2.保存一个模板时,需要在保存完毕后将改动合并到Page.dt中
			function updatePageDt(){
				for(var x in Page.newDT)
					if(typeof Page.newDT[x]!="function"){
						if (Page.newDT[x]==0)
							delete Page.dt[x];
						else
							Page.dt[x] = Page.newDT[x];
					}
				updateDtInfo();
				Page.newDT = {};
				setFlag(false);
			}
			//设置是否有改动的flag,主要用于更友好的页面提示
			function setFlag(flag){
				Page.flag = flag;
				var status = $('status');
				if(flag)
					status.innerHTML = "<span class='des'>*</span>";
				else
					status.innerHTML = "";
			}
			//保存一个新模板
			function saveAs(){
				if(!checkForm(Page.form.id))
					return;
				var name = prompt("请输入保存模板的名字:",'默认');
				if (name){
					checkChanges();
					updatePageDt();
					try{
						var result = DWR.syncCall("txQueryService.createDistributeTemplate",
							Page.cardUserId,Page.accCode,Page.distribute,name,Page.dt);
						Page.templateId = result.id;
						Page.templateName = result.name;
						Page.lastModified = result.lastModified;
						updatePageDt();
					}catch(e){
						showError(e.description);
					}
					Page.changed = true;
				}
			}
			function onSave(){
				saveTemplate();
				feedback();
			}
			function cancel(){
				confirmLeave();
				feedback();
			}
			function feedback(){
				dialogBackValue(Page.changed);
				this.close();
			}
		</script>

	</head>
	<body onload="load();" onunload="cancel();" class="noScroll">
		<table cellSpacing=0 cellPadding=0 width="100%" border=0 height="100%">
			<tr>
				<td width="25%" class="treeBar treeBar">单位部门</td>
				<td width="75%" class="treeBar treeBar">
					当前模板:[<span id="templateName"></span>] 保存于[<span id="lastModified"></span>]
					<span id="status"></span>
				</td>
			</tr>
			<tr id="treeBody" class="treeBody">
				<td valign="top" width="25%">
					<div class="scrollDiv">
						<div id="depttree" class="treeDiv"></div>
					</div>
				</td>
				<td valign="top" width="75%">
					<form id="listForm" name="listForm">
						<div class="searchForm">
							<table cellSpacing="0" cellPadding="4" width="100%" border="0">
								<tr>
									<td>
										<label>卡&nbsp;&nbsp;&nbsp;&nbsp;号</label> 
										<input type="text" name="cardAsn" value="" onblur="checkV(this,'asn')">
									</td>
									<td>
										<label>司机姓名</label> 
										<input type="text" name="customerName" value="">
									</td>
									<td>
										<button onclick="Page.qt.showTable()">查 询</button>
									</td>
								</tr>
							</table>
						</div>
						<div id="datalist"></div>
					</form>
				</td>
			</tr>
		</table>
	</body>
</html>